<template>
  <header :class="['header', { shrink: isShrink }]">
    <div class="logo" @click="gotoPage('/blog')">
      <span class="logo-text">{{ displayedText }}</span>
      <div class="cursor"></div>
    </div>
    <nav class="nav-links">
      <template v-for="route in data" :key="route.path">
        <a v-if="route.meta?.hidden === false" @click="gotoPage(route.path)">
          {{ route.meta?.title }}
        </a>
      </template>
    </nav>
    <div class="nav-right">
      <el-icon :size="40" color="#fff" @click="drawer = !drawer">
        <Fold v-if="!drawer" />
        <CloseBold v-else />
      </el-icon>
    </div>

    <el-drawer v-model="drawer" :direction="'rtl'" :size="'20%'" style="background: #000">
      <nav class="drawer-nav">
        <a v-for="link in drawLinks" :key="link" href="#" @click="drawer = false">{{ link }}</a>
      </nav>
    </el-drawer>
  </header>
</template>
<script setup lang="ts">
import { ref, onMounted, onBeforeUnmount } from "vue";
import { Fold, CloseBold } from "@element-plus/icons-vue";
import type { RouteRecordRaw } from "vue-router";
import { useRouter } from "vue-router";
const router = useRouter();
const props = defineProps({
  data: {
    type: Array<RouteRecordRaw>,
    required: true,
    default: () => [],
  },
  basePath: {
    type: String,
    required: true,
    example: "/blog",
  },
});

// 响应式状态
const isShrink = ref(false);
const displayedText = ref("");
// const navLinks = ["博客文章", "项目实践", "订阅本站", "个人简历"];
const drawLinks = ["访客授权", "后台管理", "联系站长"];
const text = "HyperSense";
// 滚动相关变量
const lastScroll = ref(0);
const shrinkHeight = ref(window.innerHeight * 0.9);
// 在原有响应式状态后添加
const drawer = ref(false);
// 打字机效果
let typeIndex = 0;
let typeTimeout: number;

const typeWriter = () => {
  if (typeIndex < text.length) {
    displayedText.value += text.charAt(typeIndex);
    typeIndex++;
    typeTimeout = setTimeout(typeWriter, 150);
  }
};

// 滚动处理
const handleScroll = () => {
  const currentScroll = window.pageYOffset;

  if (currentScroll > lastScroll.value) {
    isShrink.value = true;
    // 当抽屉打开时下滑关闭抽屉
    if (drawer.value) {
      drawer.value = false;
    }
  } else {
    isShrink.value = currentScroll >= shrinkHeight.value;
  }
  lastScroll.value = currentScroll;
};
function gotoPage(path: String) {
  console.log("拿到地址了" + path);
  // 将 String 类型转换为 string 类型
  router.push(path.toString()); // 跳转到 /about 页面
}

// 生命周期钩子
onMounted(() => {
  console.log(props.data);
  typeWriter();
  window.addEventListener("scroll", handleScroll);
});

onBeforeUnmount(() => {
  clearTimeout(typeTimeout);
  window.removeEventListener("scroll", handleScroll);
});
</script>

<style lang="scss" scoped>
/* 保留原有CSS样式 */
.header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  padding: 2rem 5rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: rgba(0, 0, 0, 0);
  transition:
    all 0.5s ease-in-out,
    opacity 0.5s ease-in-out;
  opacity: 1;
  visibility: visible;
  z-index: 9999;
  @media (max-width: 960px) {
    justify-content: center;
    padding: 1.5rem 2rem !important;

    .logo {
      flex-grow: 1;
      justify-content: center;
      order: 2; /* 将logo放在中间 */
    }

    .nav-links {
      display: none; /* 隐藏桌面导航 */
    }

    .nav-right {
      order: 3; /* 右侧图标保持最右位置 */
      flex-grow: 0;
    }
  }
}

.header.shrink {
  padding: 2rem 5rem;
  opacity: 0;
  visibility: hidden;
  transition:
    all 0.5s ease-in-out,
    opacity 0.8s ease-in-out;
}

.logo {
  font-size: 2rem;
  color: #fff;
  display: flex;
  align-items: center;
  font-weight: bolder;
  font-family: "Courier New", monospace;
}

.cursor {
  display: inline-block;
  width: 2px;
  height: 1.2em;
  background: #fff;
  margin-left: 4px;
  animation: blink 0.8s infinite;
}

.nav-links {
  position: fixed;
  left: 25rem;
  display: flex;
  gap: 2rem;
}

.nav-links a {
  color: #fff;
  text-decoration: none;
  font-size: 1.2rem;
  transition: color 0.3s;
  font-weight: 600;
  position: relative; /* 添加相对定位 */
}

.nav-links a:hover {
  color: #cccccc; /* 修改悬停颜色为科技感青色 */
  &::after {
    content: "";
    position: absolute;
    left: 0;
    bottom: -5px;
    width: 100%;
    height: 2px;
    background: #cccccc;
    animation: underline-grow 0.3s ease-out forwards;
  }
}

@keyframes underline-grow {
  from {
    transform: scaleX(0);
  }
  to {
    transform: scaleX(1);
  }
}

@keyframes blink {
  0%,
  100% {
    opacity: 1;
  }
  50% {
    opacity: 0;
  }
}

.nav-right {
  display: flex;
  align-items: right;
  cursor: pointer;
  gap: 1rem;
}

.drawer-nav {
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
  padding: 20px;

  a {
    display: flex;
    align-items: center;
    color: #fff;
    text-decoration: none;
    font-size: 1.1rem;
    transition: color 0.3s;
    justify-content: center;
    width: 100%;
    &:hover {
      color: #cccccc;
    }
  }
}

@media (max-width: 960px) {
  .nav-right {
    position: static;
    margin-left: auto; /* 保持图标右对齐 */
  }

  .el-drawer {
    :deep(.el-drawer__header) {
      margin-bottom: 2rem;
    }
  }
}
</style>
